package com.lc.hongtai.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonBorrowindex;

import java.util.List;

/**
 * Created by Administrator on 2017/3/31.
 */

public class BorrowPop extends PopupWindow {

    private static final String TAG = "BorrowPop";
    private final Context context;
    private final int height;
    private ListView listView;
    private MyAdapter adapter;

    public BorrowPop(Context context) {
        super(context);
        this.context = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        this.height = wm.getDefaultDisplay().getHeight();

        initViews(context);
    }

    private void initViews(Context context) {

        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setTouchable(true);
        this.setAnimationStyle(R.style.PopupAnimation);
        this.setTouchable(true); // 设置popupwindow可点击
        this.setOutsideTouchable(true); // 设置popupwindow外部可点击
        this.setFocusable(true); // 获取焦点

        View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pop_menulist, null);
        this.listView = (ListView) view.findViewById(R.id.menulist);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0,0,0,100);
        adapter = new MyAdapter(context, 0);
        listView.setAdapter(adapter);
        listView.setLayoutParams(layoutParams);
    }

    public void show(final View view, List<JsonBorrowindex.Info.ListContent> list) {
        adapter.clear();
        adapter.addAll(list);
        view.post(new Runnable() {
            @Override
            public void run() {
                float y = view.getBottom();
                Log.d(TAG, "--> show:y = " + y);
            }
        });
        float y = view.getY();
        float v = height - y;
        int i = (int) (height - 100 - v);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        showAsDropDown(view);

    }

    private class MyAdapter extends ArrayAdapter<JsonBorrowindex.Info.ListContent> {

        public MyAdapter(Context context, int resource) {
            super(context, resource);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = ((TextView) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_list_item, parent, false));
            view.setText(getItem(position).name);
            return view;
        }
    }
}
